<?php 

/**
 *	@name SmartXMLElement
 *	@copyright : SMART - ULg (www.smart.ulg.ac.be)		
 *
**/


/**
 * Class SmartXMLElement : 
 * <p># explain what I do ...</p>
 * 
 * <pre>
 * $Revision: 1.4 $ 
 * $Author: jph $
 * $Date: 2007/06/20 14:27:55 $
 * $RCSfile: SmartXMLElement.php,v $
 * </pre>
 *
 * @name SmartXMLElement
 * @package smart.exams.core.utils.smartXML
 * @author : JPh HUMBLET
 * @copyright : SMART - ULg (www.smart.ulg.ac.be)
 * @category : e-C&QCST platform
 *
**/

class  SmartXMLElement extends SimpleXMLElement
{
	
	/**
	 * @name validateSchema
	 * validate this XML against a given XMLSchema
	 *
	 * @param $xsd the xmlSchema definition
	 * @return boolean
	**/
	public function validateSchema($xsd) {
		$xml = $this->asXML();
		error_log($xsd);
		$dom = new domDocument;
		$dom->loadXML($xml);
		if ( !$dom->schemaValidateSource($xsd) ){ 
			$e = $php_errormsg;
			throw new Exception("Schema Validation Failed. " . $e, 29);
			return false;
		} 
		return true;
	}
	
	/**
	 * @name validateSchemaByURI
	 * FunctionDescription
	 *
	 * @param string $schemaURI
	 * @return boolean
	**/
	public function validateSchemaByURI( $schemaURI ) {
		$schema_file = $schemaURI . ".xsd";
		$sch = fopen($schema_file, "r");
		$xmlSchema = stream_get_contents($sch);
		fclose($sch);
		return $this->validateSchema($xmlSchema);
	}
	
	/**
	 * @name appendSXE
	 * appends the current element with SimpleXMLElement
	 *
	 * @param $sxe theSimpleXMLElement to be appended
	 * @return boolean
	**/
	public function appendSXE($sxe) {
		$dom_sxe = dom_import_simplexml($sxe);
	 	return $this->appendDOM($dom_sxe);
	}
	
	/**
	 * @name appendDOM
	 * Fappends the current element with a domNone
	 *
	 * @param 
	 * @return returnDescription
	**/
	private function appendDOM($dom) {
		$node1 = dom_import_simplexml($this);
		$node2 = $node1->ownerDocument->importNode($dom, true);
		$node1->appendChild($node2);
		return simplexml_import_dom($node2, get_class($this));
	}
	
	/**
	 * @name appendXML
	 * ppends the current element with an xml string
	 *
	 * @param $xmls the xml string
	 * @return boolean
	**/
	public function appendXML($xmls) {
		$sxe = new SimpleXMLElement( $xmls );
		return $this->appendSXE($sxe);
	}

	/**
	 * @name setNamespace
	 * sets a namespace for the current element
	 *
	 * @param $nsURI the namespace URI
	 * @param $nsQualifier the namespace qualifier
	 * @return boolean
	**/
	public function addNamespace($nsURI, $nsQualifier = "") {
		if ( $nsQualifier == "" ){ 
			$attr = 'xmlns';
		} else {
			$attr = 'xmlns:'.$nsQualifier;
		}
		$this->addAttribute("a","a",$nsURI);
		return true;
	}

}


?>
